package cn.good.biz.controller;

import cn.good.biz.service.GDCollectService;
import cn.good.dto.Dto;
import cn.good.dto.DtoUtil;
import cn.good.pojo.Collect;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.jws.Oneway;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/doc/collect")
public class CollectController {

    @Resource
    private GDCollectService gdCollectService;


    @ApiOperation(value="收藏文章",httpMethod = "POST",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class,notes="收藏文章")
    @RequestMapping("/collect")
    public Dto attention(@RequestBody Collect collect) {
        if(collect.getUserId()==null || collect.getCollectId()==null){
            return DtoUtil.returnFail("用户Id和文章Id不能为空","20011");
        }
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("userId", collect.getUserId());
        param.put("collectId", collect.getCollectId());
        Collect collect1 = null;
        try {
            collect1 = gdCollectService.findCollectByParam(param);
            if (collect1 == null) {
                collect.setType(0);
                gdCollectService.insertCollect(collect);
                return DtoUtil.returnSuccess("收藏成功");
            }
            if (collect.getType() == 1) {
                collect.setType(0);
                gdCollectService.articleCollect(collect);
                return DtoUtil.returnSuccess("收藏成功");
            } else if (collect.getType() == 0) {
                collect.setType(1);
                gdCollectService.articleCollect(collect);
                return DtoUtil.returnSuccess("取消收藏成功");
            }else {
                return DtoUtil.returnFail("收藏类型有误","20009");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败", "20010");
        }
    }
}
